package no.weather;

import java.util.ArrayList;
import java.util.List;

import no.weather.weatherProvider.WeatherDatabase;
import no.weather.weatherProvider.WeatherProvider;
import no.weather.weatherProxy.wsKlima.WsKlimaProxy;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class StationMap extends MapActivity {
	MyLocationOverlay mMyLocationOverLay;
	WeatherProvider mProvider;

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.station_map_view);
		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mProvider = new WeatherProvider(this);

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = getResources().getDrawable(R.drawable.wind);
		String columns[] = new String[] { WeatherDatabase.STATION_ID,
				WeatherDatabase.STATION_NAME,
				WeatherDatabase.STATION_LONGTITUDE,
				WeatherDatabase.STATION_LATITUDE };
		Cursor stations = mProvider.getStationsFromProvider(
				WsKlimaProxy.PROVIDER, columns);
		StationItemizedOverlay itemizedoverlay = new StationItemizedOverlay(
				drawable, this, WsKlimaProxy.PROVIDER, stations);
		mapOverlays.add(itemizedoverlay);
		mMyLocationOverLay = new MyLocationOverlay(getApplicationContext(),
				mapView);
		mMyLocationOverLay.enableMyLocation();
		mapOverlays.add(mMyLocationOverLay);
	}

	@Override
	protected void onPause() {
		mMyLocationOverLay.disableMyLocation();
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMyLocationOverLay.enableMyLocation();
	}

	private class StationItemizedOverlay extends ItemizedOverlay<OverlayItem> {
		private static final String LOG_ID = "no.weather.StationMap.StationOverlay";
		private Context mContext;
		private List<OverlayItem> mOverlays;
		private String mProvider;
		private Integer mSize;

		public StationItemizedOverlay(Drawable defaultMarker, Context context,
				String provider, Cursor stations) {
			super(boundCenterBottom(defaultMarker));
			Log.v(LOG_ID, "constructor called");
			mContext = context;
			mProvider = provider;
			mSize = stations.getCount();
			mOverlays = new ArrayList<OverlayItem>(mSize);
			setOverLayItem(stations);
		}

		@Override
		protected OverlayItem createItem(int i) {
			Log.v(LOG_ID, "createItem is called for item: " + i);
			return mOverlays.get(i);
		}

		@Override
		protected boolean onTap(final int index) {
			Log.v(LOG_ID, "On tap called");
			OverlayItem item = mOverlays.get(index);
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(item.getTitle());
			dialog.setMessage("ID:" + item.getSnippet() + "\nProvider: "
					+ mProvider);
			dialog.setCancelable(true);
			dialog.setNegativeButton(getResources().getString(R.string.Close),
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
			dialog.setPositiveButton(
					getResources().getString(R.string.Open_station),
					new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							OverlayItem item = mOverlays.get(index);
							Intent intent = new Intent(StationMap.this,
									WeatherReportView.class);
							intent.putExtra(WeatherDatabase.STATION_ID,
									item.getSnippet());
							intent.putExtra(WeatherDatabase.STATION_PROVIDER,
									mProvider);
							startActivity(intent);

						}
					});
			dialog.show();
			return true;
		}

		private void setOverLayItem(Cursor stations) {
			Log.v(LOG_ID, "setOverLayItem");
			Integer latCol = stations
					.getColumnIndexOrThrow(WeatherDatabase.STATION_LATITUDE);
			Integer lonCol = stations
					.getColumnIndexOrThrow(WeatherDatabase.STATION_LONGTITUDE);
			Integer nameCol = stations
					.getColumnIndexOrThrow(WeatherDatabase.STATION_NAME);
			Integer idCol = stations
					.getColumnIndexOrThrow(WeatherDatabase.STATION_ID);

			stations.moveToFirst();
			for (int i = 0; i < stations.getCount(); i++) {
				Integer lat = (int) (1e6 * stations.getDouble(latCol));
				Integer lon = (int) (1e6 * stations.getDouble(lonCol));
				String name = stations.getString(nameCol);
				String id = stations.getString(idCol);
				mOverlays
						.add(new OverlayItem(new GeoPoint(lat, lon), name, id));
				stations.moveToNext();
			}

			populate();

		}

		@Override
		public int size() {
			// Log.d(LOG_ID, "size is called, the size is:"+mSize);
			return mSize;
		}
	}
}
